package com.xuxueli.applyModules.controller;
import com.xuxueli.applyModules.commont.QuartzSchedulerCom;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * 这里并没有采用restful风格 只是简单封装了一下api
 */
@RestController
@RequestMapping("/quartz")
public class QuartzApiController {

    @Resource
    private QuartzSchedulerCom quartzSchedulerCom;

    /**
     * 开始执行所有任务
     */
    @RequestMapping("/start")
    public void startQuartzJob() {
        try {
            quartzSchedulerCom.startAllJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 开始执行某个任务
     */
    @GetMapping("/startJob")
    public void startJob(@RequestParam String name, @RequestParam String group,
                         @RequestParam(value = "cron",required = false,defaultValue = "0/5 * * * * ?") String cron) {
        try {
            quartzSchedulerCom.startJob(name,group,cron);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取Job信息
     * @param name
     * @param group
     * @return
     */
    @GetMapping("/info")
    public String getQuartzJob(@RequestParam String name, @RequestParam String group) {
        String info = null;
        try {
            info = quartzSchedulerCom.getJobInfo(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 修改某个任务的执行时间
     * @param name
     * @param group
     * @param time
     * @return
     */
    @GetMapping("/modify")
    public boolean modifyQuartzJob(@RequestParam String name, @RequestParam String group,
                                   @RequestParam String time) {
        boolean flag = true;
        try {
            flag = quartzSchedulerCom.modifyJob(name, group, time);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 暂停某个任务
     * @param name
     * @param group
     */
    @GetMapping(value = "/pause")
    public void pauseQuartzJob(@RequestParam String name, @RequestParam String group) {
        try {
            quartzSchedulerCom.pauseJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 暂停所有任务
     */
    @GetMapping(value = "/pauseAll")
    public void pauseAllQuartzJob() {
        try {
            quartzSchedulerCom.pauseAllJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 恢复某个任务
     */
    @GetMapping(value = "/resumeJob")
    public void pauseAllQuartzJob(@RequestParam String name, @RequestParam String group) {
        try {
            quartzSchedulerCom.resumeJob(name,group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }


    /**
     * 删除某个任务
     * @param name
     * @param group
     */
    @DeleteMapping(value = "/delete")
    public void deleteJob(@RequestParam String name, @RequestParam String group) {
        try {
            quartzSchedulerCom.deleteJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

}
